package com.hqj.util;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import javax.swing.text.html.parser.Entity;

public class Generator {

    public static void main(String[] args) {
        String basePackge = "com.hqj";
        boolean flat = false;

        createBeanByTable(basePackge);
    }

    public static void createBeanByTable(String basePackge){
        AutoGenerator mpg = new AutoGenerator();

        GlobalConfig config = new GlobalConfig();
        String dbUrl = "jdbc:mysql://192.168.3.133:3306/sct?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
       //数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root") //数据库用户名
                .setPassword("root") //数据库密码
                .setDriverName("com.mysql.cj.jdbc.Driver"); //数据库驱动
        //策略
        StrategyConfig sc = new StrategyConfig();
        sc.setCapitalMode(true)
                .setEntityLombokModel(false) //是否实现的实体不添加get/set的方法 lombok是一个不需要get/set方法都能赋值的插件
                .setNaming(NamingStrategy.underline_to_camel)//下划线对应驼峰命名
                //.setTablePrefix("t_")
                .setInclude("t_role_menu");//数据表的名称

        config.setAuthor("hqj")
                .setOutputDir("D:\\Workspaces\\Idea\\SCT\\src\\main\\java")
                .setFileOverride(false) //是否覆盖文件,默认false
                .setActiveRecord(true) //是否需要ActiveRecord特性
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(false)
                .setMapperName("%sDao") //生成dao的名称后缀为Dao
                .setXmlName("%sMapper"); //生成的xml文件后缀为Mapper

        PackageConfig pc = new PackageConfig();
        pc.setParent(basePackge);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.setGlobalConfig(config);
        mpg.setDataSource(dsc);
        mpg.setStrategy(sc);
        mpg.setPackageInfo(pc);

        mpg.execute();
    }
}
